package club.dataresources.service.impl;

import club.dataresources.entity.Product;
import club.dataresources.entity.ProductExample;
import club.dataresources.mapper.ProductMapper;
import club.dataresources.service.ProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author
 * @create 2018-11-04 20:50
 **/
@Service
public class ProductServiceImpl implements ProductService{
    @Autowired
    private ProductMapper productMapper;


    @Override
    public boolean addProduct(Product product) throws Exception {
        return productMapper.insertSelective(product)==1;
    }

    @Override
    public boolean removeProduct(Integer productId) throws Exception {
        return productMapper.deleteByPrimaryKey(productId)==1;
    }

    @Override
    public boolean updateProduct(Product product) throws Exception {
        return productMapper.updateByPrimaryKeySelective(product)==1;
    }

    @Override
    public Product findProByPid(Integer productId) throws Exception {
        return productMapper.selectByPrimaryKey(productId);
    }

    @Override
    public Page<Product> listProduct(Integer page, Integer rows,Product product) throws Exception {
        PageHelper.startPage(page, rows);
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();

        //如果是模糊查询
        if(product.getProductBrand()!=null){
            criteria.andProductBrandLike(product.getProductBrand());
        }else if(product.getProductLevel()!=null){
            criteria.andProductLevelLike(product.getProductLevel());
        }else if(product.getProductName()!=null){
            criteria.andProductNameLike(product.getProductName());
        }

        criteria.andProductIdIsNotNull();
        return productMapper.selectByExample(example);
    }
}
